package com.intel.service.Impl;

import com.intel.service.ClientService;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.springframework.beans.factory.annotation.Value;

public class ClientServiceImpl implements ClientService {

    @Value("${wsdlUrl}")
    private String wsdlUrl;

    @Value("${namespaceURI}")
    private String namespaceURI;

    @Value("${userLogin}")
    private String userLogin;

    @Value("${uploadMasterData}")
    private String uploadMasterData;

    //安全认证
    public OMElement authenticateWebservice(OMElement request) throws AxisFault{
        return clientInvoke(request, userLogin);
    }

    //上传数据
    public OMElement uploadWebservice(OMElement request) throws AxisFault{
        return clientInvoke(request, uploadMasterData);
    }

    private OMElement clientInvoke(OMElement request, String methodName) throws AxisFault {
            RPCServiceClient client = new RPCServiceClient();
            Options option = client.getOptions();
            //endpoint address
            EndpointReference erf = new EndpointReference(wsdlUrl);
            option.setTo(erf);
            client.setOptions(option);

            //指定method
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace namespace = fac.createOMNamespace(namespaceURI,"");
            OMElement method = fac.createOMElement(methodName, namespace);
            method.addChild(request);
            method.build();
            return client.sendReceive(method);
    }
}
